Grokking Oracle Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Oracle Easy
Count Number of Pairs With Absolute Difference K (easy)
Solution: Count Number of Pairs With Absolute Difference K
Degree of an Array (easy)
Solution: Degree of an Array
Find Subsequence of Length K With the Largest Sum (easy)
Solution: Find Subsequence of Length K With the Largest Sum
Rectangle Overlap (easy)
Solution: Rectangle Overlap
Remove All Adjacent Duplicates In String (easy)
Solution: Remove All Adjacent Duplicates In String
Island Perimeter (easy)
Solution: Island Perimeter
Climbing Stairs (easy)
Solution: Climbing Stairs
Move Zeroes (easy)
Solution: Move Zeroes
Excel Sheet Column Title (easy)
Solution: Excel Sheet Column Title
Valid Parentheses (Easy)
Solution: Valid Parentheses
Oracle Medium
Longest Palindromic Substring (medium)
Solution: Longest Palindromic Substring
Single Element in a Sorted Array (medium)
Solution: Single Element in a Sorted Array
Pairs of Songs With Total Durations Divisible by 60 (medium)
Solution: Pairs of Songs With Total Durations Divisible by 60
Search a 2D Matrix II (medium)
Solution: Search a 2D Matrix II
Minimize Maximum of Array (medium)
Solution: Minimize Maximum of Array
Asteroid Collision (medium)
Solution: Asteroid Collision
Combination Sum (medium)
Solution: Combination Sum
Binary Tree Right Side View (medium)
Solution: Binary Tree Right Side View
Sort Colors (medium)
Solution: Sort Colors
Group Anagrams (Medium)
Solution: Group Anagrams
Decode String (Medium)
Solution: Decode String
Number of Islands (medium)
Solution: Number of Islands
Binary Tree Zigzag Level Order Traversal (medium)
Solution: Binary Tree Zigzag Level Order Traversal
Longest Common Subsequence (medium)
Solution: Longest Common Subsequence
Rotting Oranges (medium)
Solution: Rotting Oranges
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Longest Substring Without Repeating Characters (medium)
Solution: Longest Substring Without Repeating Characters (medium)
Search in Rotated Array (medium)
Solution: Search in Rotated Array
Spiral Matrix (medium)
Solution: Spiral Matrix
Container With Most Water (Medium)
Solution: Container With Most Water
3Sum (medium)
Solution: 3Sum
Simplify Path (medium)
Solution: Simplify Path
Next Permutation (medium)
Solution: Next Permutation
Basic Calculator II (medium)
Solution: Basic Calculator II
Merge Intervals (medium)
Solution: Merge Intervals
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Validate Binary Search Tree (medium)
Solution: Validate Binary Search Tree
Oracle Hard
Integer to English Words (hard)
Solution: Integer to English Words
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Merge K Sorted Lists (hard)
Solution: Merge K Sorted Lists
Max Points on a Line (hard)
Solution: Max Points on a Line
Sudoku Solver (hard)
Solution: Sudoku Solver
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Solution: First Missing Positive

Problem Statement

Given an unsorted array nums containing positive and negative integers, return the smallest missing positive integer value.

Note: You must implement algorithm in time and uses space.

Examples

  • Example 1:

    • Input: [5, 3, -1, 8, 4, 2]
    • Expected Output: 1
    • Justification: The first missing positive integer in the array is 1.
  • Example 2:

    • Input: [2, 5, -7, 3, 9, 4, 6, 1]
    • Expected Output: 7
    • Justification: The array contains all integers from 1 to 6, so the first missing positive integer is 7.
  • Example 3:

    • Input: [-4, -3, -2, -1]
    • Expected Output: 1
    • Justification: Since there are no positive integers, the first missing positive integer is 1.

Solution

To solve this problem, we'll employ an in-place algorithm to rearrange the elements such that each positive integer (i) is placed at the index (i-1), ignoring negative numbers and numbers larger than the array's length. This approach ensures that if a positive integer is missing, its corresponding index will not hold the correct value.

Our solution is effective because it allows us to identify the missing positive integer with a single pass through the array to rearrange the elements, followed by another pass to find the first index not holding its corresponding positive integer. This method avoids the need for extra space for another data structure, making it efficient in terms of both time and space complexity.

Step-by-step Algorithm

  • Initialize a variable n to store the length of the array.
  • First Pass: Iterate over the array.
    • For each element num at index i, while num is within the range [1, n] and the element at num-1 is not num, swap num with the element at num-1.
  • Second Pass: Iterate over the array again.
    • If the element at index i is not i+1, return i+1.
  • If all positions contain the correct integers, return n+1 to indicate that the first missing positive is just after the last index.

Algorithm Walkthrough

Let's consider the input: [2, 5, -7, 3, 9, 4, 6, 1]

Step 1: Start with the first element (2)

  • Element is 2; it should be at index 1 (since we are aiming for (i) at (i-1) index, considering 1-based indexing for the target positions).
  • Swap 2 with the element at its target index (1), which is 5.
  • Array after swap: [5, 2, -7, 3, 9, 4, 6, 1]

Step 2: Look at the element now at index 0 (5)

  • Element is 5; it should be at index 4.
  • Swap 5 with the element at index 4 (9).
  • Array after swap: [9, 2, -7, 3, 5, 4, 6, 1]

Step 3: Process the element now at index 0 (9)

  • Element is 9; it's out of range for our placement (since there is no index 8), so we leave it as is.

Step 4: Move to the second element (2)

  • Element is 2; it should be at index 1.
  • Swap 2 with the element at index 1 (itself in this case, no action needed).
  • Array state remains: [9, 2, -7, 3, 5, 4, 6, 1]

Step 5: Move to the third element (-7)

  • Element is -7; negative numbers are ignored.

Step 6: Move to the fourth element (3)

  • Element is 3; it should be at index 2.
  • Swap 3 with the element at index 2 (-7).
  • Array after swap: [9, 2, 3, -7, 5, 4, 6, 1]

Step 7: Move to the element now at index 3 (-7)

  • Element is -7; negative numbers are ignored.

Step 8: Move to the fifth element (5)

  • Element is 5; it should be at index 4.
  • It's already in the correct position.

Step 9: Move to the sixth element (4)

  • Element is 4; it should be at index 3.
  • Swap 4 with the element at index 3 (-7).
  • Array after swap: [9, 2, 3, 4, 5, -7, 6, 1]

Step 10: Move to the seventh element (6)

  • Element is 6; it should be at index 5.
  • Swap 6 with the element at index 5 (-7).
  • Array after swap: [9, 2, 3, 4, 5, 6, -7, 1]

Step 11: Move to the third element (-7)

  • Element is -7; negative numbers are ignored.

Step 12: Move to the eighth element (1)

  • Element is 1; it should be at index 0.
  • Swap 1 with the element at index 0 (9).
  • Array after swap: [1, 2, 3, 4, 5, 6, -7, 9]

Final Array State

[1, 2, 3, 4, 5, 6, -7, 9]

Finding the First Missing Positive

  • Start from the beginning of the array, checking if the element matches its index + 1.
  • All elements from 1 to 6 are correctly placed.
  • The first missing positive integer, given our repositioning, is at index 6 (considering 0-based indexing), where we expect to find 7, but we find -7.

Result

After rearranging the array and checking for the first non-matching index, the first missing positive integer for the array [2, 5, -7, 3, 9, 4, 6, 1] is determined to be 7.

Code

Java

. . . .

Complexity Analysis

Time Complexity

  • : The algorithm passes through the array a constant number of times (specifically, two passes). Each pass is linear in terms of the number of elements in the array. Therefore, the total operations are proportional to the number of elements, (n).

Space Complexity

  • : The space complexity is constant because the algorithm rearranges the elements in place without using any additional data structures that grow with the size of the input array.
First Missing Positive (hard)
Merge K Sorted Lists (hard)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Final Array State

Code

Complexity Analysis

Time Complexity

Space Complexity